<?php
class Product_Model_Putaway
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Product_Model_DbTable_Putaway();
	}

	public  function create($row){	 
 		return $this->_table->insert($row); 
	}
	
	public  function update($row,$id){
		$where = $this->_table->getAdapter()->quoteInto("putaway_id= ?", $id);
		return $this->_table->update($row, $where);
	}
	
	public function updateQtyByRcodeAndPid($row ,$receiving_code, $product_id){
		$where = $this->_table->getAdapter()->quoteInto("receiving_code= ?", $receiving_code);
		$where .= $this->_table->getAdapter()->quoteInto(" and product_id= ?", $product_id);
		return $this->_table->update($row, $where);
	}
	
	public function delete($id){	
		$where = $this->_table->getAdapter()->quoteInto('putaway_id = ?', $id); 
		return $this->_table->delete($where);
	}
	
	public  function updatePutawayCodeAndLot($row,$option){
		$where = $this->_table->getAdapter()->quoteInto("putaway_code= ?", $option['putaway_code']);
		$where.= $this->_table->getAdapter()->quoteInto(" and putaway_lot_number= ?", $option['putaway_lot_number']);
		return $this->_table->update($row, $where);
	}
	
	public  function getByPutawayAndLot($condition=array()){
		$where = $this->_table->getAdapter()->quoteInto("putaway_code= ?", $condition['putaway_code']);
		$where.= $this->_table->getAdapter()->quoteInto(" and putaway_lot_number= ?", $condition['putaway_lot_number']);
		return $this->_table->fetchRow($where);
	}
	
	public function getByReceivingCode($code){
		$where = $this->_table->getAdapter()->quoteInto('receiving_code= ?', $code);
		return $this->_table->fetchRow($where);
	}

	public function getByPutawayId($id){
		$where = $this->_table->getAdapter()->quoteInto('putaway_id= ?', $id);
		return $this->_table->fetchRow($where);
	}
	
    public function getByCondition($condition=array(),$type='*',$page="0",$pageSize="0",$orderBy='putaway_id desc'){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition["warehouse_id"]) && $condition['warehouse_id'] != ""){
			$select->where("warehouse_id=?",$condition['warehouse_id']);
		}
		if(isset($condition["receiving_code"]) && $condition['receiving_code'] != ""){
			$select->where("receiving_code=?",$condition['receiving_code']);
		}
		if(isset($condition["putaway_status"]) && $condition['putaway_status'] != ""){
			$select->where("putaway_status=?",$condition['putaway_status']);
		}
        //多种状态
        if(isset($condition["putaway_status_in"]) && $condition['putaway_status_in'] != ""){
            $select->where("putaway_status in(?)",$condition['putaway_status_in']);
        }
		if(isset($condition["putaway_code"]) && $condition['putaway_code'] != ""){
			$select->where("putaway_code=?",$condition['putaway_code']);
		}
		if(isset($condition["putaway_lot_number"]) && $condition['putaway_lot_number'] != ""){
			$select->where("putaway_lot_number=?",$condition['putaway_lot_number']);
		}
		if(isset($condition["ws_code"]) && $condition['ws_code'] != ""){
			$select->where("ws_code=?",$condition['ws_code']);
		}
		if(isset($condition["product_id"]) && $condition['product_id'] != ""){
			$select->where("product_id=?",$condition['product_id']);
		}
		if(isset($condition["putaway_date_from"]) && $condition['putaway_date_from'] != ""){
			$select->where("putaway_create_date>=?",$condition['putaway_date_from']);
		}
		if(isset($condition["putaway_date_to"]) && $condition['putaway_date_to'] != ""){
			$select->where("putaway_create_date<=?",$condition['putaway_date_to']);
		}
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
			$select->order($orderBy);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}
	
	/**
	 * 获取当天最后一个 putaway_code
	 */
	public function getCurdateLastCode(){
		$select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'max(putaway_code)');
        $mindate = date('Y-m-d').' 00:00:00';
        $maxdate = date('Y-m-d').' 23:59:59';
        $select->where("putaway_create_date > ?",$mindate);
        $select->where("putaway_create_date < ?",$maxdate);
		return $this->_table->getAdapter()->fetchOne($select);
	}
}